package com.sina.shop.domain.menu;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.sina.shop.core.domain.Domain;
import com.sina.shop.domain.goods.Goods;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.validation.constraints.Min;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description: 菜单实体类--树状结构
 * 段浩杰   2017-12-08-18:33
 */
@Entity
@Table(name = "tb_menu")
@DynamicUpdate
@DynamicInsert
public class Menu extends Domain {

    @Id
    @Column(name = "id", updatable = false)
    @GenericGenerator(name = "sequence_id", strategy = "com.sina.shop.sequence.IdGenerator")
    @GeneratedValue(generator = "sequence_id")
    @Min(1)
    private Long id;//id

    @ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
    @JoinColumn(name = "mid")
    @JsonIgnore
    private Menu menu;//父节点

    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "menu", fetch = FetchType.LAZY)
    private Set<Menu> menus;//所有的子节点

    @Column(name = "name", length = 20)
    private String name;//名字

    @Column(name = "sort", length = 5)
    private Integer sort;//排序

    @Column(name = "create_time")
    @ColumnDefault("CURRENT_TIMESTAMP")
    @DateTimeFormat(pattern = "uuuu-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)//此注解是为了解决feign远程调用中出现的LocalDateTime不能序列化问题
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime createTime;//创建时间

    @Column(name = "update_time")
    @ColumnDefault("CURRENT_TIMESTAMP")
    @DateTimeFormat(pattern = "uuuu-MM-dd HH:mm:ss")
    @JsonSerialize(using = LocalDateTimeSerializer.class)//此注解是为了解决feign远程调用中出现的LocalDateTime不能序列化问题
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime updateTime;//更新时间

    @Column(name = "create_name", length = 20)
    private String createName;//创建者

    @JsonIgnore
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "menu", fetch = FetchType.LAZY)
    private Set<Goods> goods;

    @Enumerated(EnumType.STRING)
    @Column(name = "type",length = 10)
    private Type type;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    public Set<Menu> getMenus() {
        return menus;
    }

    public void setMenus(Set<Menu> menus) {
        this.menus = menus;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public Set<Goods> getGoods() {
        return goods;
    }

    public void setGoods(Set<Goods> goods) {
        this.goods = goods;
    }
}
